﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace PlcLib.Model
{
   
    public partial class Net
    {
        /// <summary>
        /// 读取最大超时毫秒
        /// </summary>
        public int MaxMilliseconds { get; set; } = 5000; // 最大毫秒数
        /// <summary>
        /// 下位机ip地址 192.168.0.1
        /// </summary>
        public string Ip { get; set; } = "192.168.0.1";

        /// <summary>
        /// x86下位机端口号
        /// </summary>
        public int Port { get; set; } = 502;
        public IPAddress iPAddress
        {
            get
            {
                if (IPAddress.TryParse(Ip, out IPAddress ipAddress))// Validate the IP address format
                {
                    return ipAddress;
                }
                else
                {
                    throw new FormatException($"{Ip}不能转换成IPAddress对象");
                }
            }
        }

        public byte ServerLastNode => iPAddress.GetAddressBytes().Last();/// 下位机ip地址的最后一个节点
        public byte ClientLastNode => LocaliPAddress.GetAddressBytes().Last();/// 上位机ip地址的最后一个节点
                                                                            
        public string LocalIp { get; set; } = "127.0.0.1";

        /// <summary>
        /// x86上位机端口号
        /// </summary>
        public int LocalPort { get; set; } = 502;
        public IPAddress LocaliPAddress
        {
            get
            {
                if (IPAddress.TryParse(LocalIp, out IPAddress localAddress))// Validate the IP address format
                {
                    return localAddress;
                }
                else
                {
                    throw new FormatException($"{LocalIp}不能转换成IPAddress对象");
                }
            }
        }
    }
}
